package tracksuite.presentation;

import java.sql.Timestamp;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import tracksuite.bussiness.ReportOfServiceBO;
import tracksuite.exception.DAOException;
import tracksuite.exception.GenericException;
import tracksuite.integration.reportofservice.ReportOfServiceTO;

public class ReportOfServiceBean extends GenericBean<ReportOfServiceBO> {

    private String title;
    private String description;

    public ReportOfServiceBean() {
        this.bo = new ReportOfServiceBO();
    }
    
    @Override
    public String actionInsert() {
        try {
        	
        	HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        	if(request.getParameter("descriptionEditor") == null){
        		throw new GenericException("Description is null.");
        	}
        	setDescription(request.getParameter("descriptionEditor"));
        	
            ReportOfServiceTO reportOfServiceTO = new ReportOfServiceTO();
            reportOfServiceTO.setTitle(getTitle());
            reportOfServiceTO.setDescription(getDescription());
            reportOfServiceTO.setDateInsert(new Timestamp(System.currentTimeMillis()));
            reportOfServiceTO.setDateUpdate(new Timestamp(System.currentTimeMillis()));
            this.bo.insert(reportOfServiceTO);
            return "OK";
        } catch (GenericException ex) {
            Logger.getLogger(CategoryBean.class.getName()).log(Level.SEVERE, null, ex);
            return "ERROR";
        }
    }

    @Override
    public String actionDelete() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String actionUpdate() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public List<ReportOfServiceTO> getAll() {
        try {
            return this.bo.getAll();
        } catch (DAOException ex) {
            Logger.getLogger(ReportOfServiceBean.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }   

}
